package EBM.UI;

import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import EBM.BLL.SocketConnection;
import EBM.BLL.Util;

public class ReviewUser extends org.eclipse.swt.widgets.Composite {

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static String userName;
	public static Display display;
	public static Shell shell;
	private Composite composite1;
	private Composite composite2;
	private TreeItem treeItem5;
	private TreeItem treeItem4;
	private TreeItem treeItem3;
	private TreeItem treeItem2;
	private TreeItem treeItem1;
	private TableColumn tableColumn1;
	private Label lblPro;
	private TableColumn tableColumn2;
	private TableColumn tableColumn3;
	private TableColumn tableColumn4;
	private TableColumn tableColumn5;
	private TableColumn tableColumn6;
	private Tree tree1;
	private Label label2;
	private Label label3;
	private Label label4;
	private Button button1;
	private Button button2;
	private Text text1;
	private Text text2;
	private Text text3;
	private Table table1;
	private MessageBox box = new MessageBox(shell, SWT.YES | SWT.ICON_WARNING);
	private MessageBox box1 = new MessageBox(shell, SWT.YES | SWT.NO | SWT.ICON_QUESTION);
	private Group group;
	
	
	ArrayList<String> AllProjectId = new ArrayList<String>();
	ArrayList<String> AllProjectName = new ArrayList<String>();
	private TreeItem treeItem6;

	private Combo cboPro;

	/*public static void main(String[] args) {
		showGUI();
	}*/
		
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static void showGUI() {
		display = Display.getDefault();
		shell = new Shell(display, SWT.CLOSE | SWT.MIN);
		ReviewUser inst = new ReviewUser(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		shell.setText("审查用户");
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.setLocation(display.getClientArea().width / 2 - shell.getSize().x/2, 
				display.getClientArea().height / 2 - shell.getSize().y/2);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public ReviewUser(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
		InitViewProList();
		InitBugByProject();
	}

	private void initGUI() {
		try {
			this.setLayout(new FormLayout());
			//this.setSize(661, 402);
			this.setSize(726, 475);
			{
				FormData composite2LData = new FormData();
				composite2LData.width = 520;
				composite2LData.height = 505;
				composite2LData.left =  new FormAttachment(216, 1000, 0);
				composite2LData.top =  new FormAttachment(1, 1000, 0);
				composite2LData.bottom =  new FormAttachment(1000, 1000, 0);
				composite2LData.right =  new FormAttachment(1000, 1000, 0);
				composite2 = new Composite(this, SWT.NONE);
				composite2.setLayout(null);
				composite2.setLayoutData(composite2LData);
				composite2.setVisible(true);
				composite2.setEnabled(true);
				{
					table1 = new Table(composite2, SWT.MULTI);
					table1.setSize(521, 355);
					table1.setBounds(0, 71, 520, 434);

					tableColumn1 = new TableColumn(table1, SWT.CENTER);
					tableColumn2 = new TableColumn(table1, SWT.CENTER);
					tableColumn3 = new TableColumn(table1, SWT.CENTER);
					tableColumn4 = new TableColumn(table1, SWT.CENTER);
					tableColumn5 = new TableColumn(table1, SWT.CENTER);
					tableColumn6 = new TableColumn(table1, SWT.CENTER);
					tableColumn1.setText("");
					tableColumn2.setText("问题单号");
					tableColumn3.setText("当前责任人");
					tableColumn4.setText("当前状态");
					tableColumn5.setText("问题概要描述");
					tableColumn6.setText("停留天数");
					tableColumn1.setWidth(18);
					tableColumn2.setWidth(80);
					tableColumn3.setWidth(80);
					tableColumn4.setWidth(80);
					tableColumn5.setWidth(150);
					tableColumn6.setWidth(70);
					table1.setHeaderVisible(true);
					table1.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							table1WidgetSelected(evt);
						}
					});
				}
				{
					lblPro = new Label(composite2, SWT.NONE);
					lblPro.setText("项目");
					lblPro.setBounds(10, 18, 53, 22);
				}
				{
					cboPro = new Combo(composite2, SWT.READ_ONLY);
					//cboPro.setBounds(49, 12, 217, 20);
					cboPro.setBounds(76, 18, 215, 23);
					cboPro.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							cboProWidgetSelected(evt);
						}
					});
				}
			}
			{
				FormData composite1LData = new FormData();
				composite1LData.width = 520;
				composite1LData.height = 405;
				composite1LData.left =  new FormAttachment(216, 1000, 0);
				composite1LData.top =  new FormAttachment(1, 1000, 0);
				composite1LData.bottom =  new FormAttachment(1001, 1000, 0);
				composite1LData.right =  new FormAttachment(1000, 1000, 0);
				composite1 = new Composite(this, SWT.NONE);
				composite1.setLayout(null);
				composite1.setLayoutData(composite1LData);

				group = new Group(composite1, SWT.NONE);
				group.setText("修改密码");
				group.setBounds(32, 23, 436, 322);
				{
					button2 = new Button(group, SWT.PUSH | SWT.CENTER);
					button2.setText("重置");
					button2.setBounds(282, 273, 85, 30);
					button2.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							text1.setText("");
							text2.setText("");
							text3.setText("");
						}
					});
				}
				{
					button1 = new Button(group, SWT.PUSH | SWT.CENTER);
					button1.setText("确定");
					button1.setBounds(117, 273, 85, 30);
					button1.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							button1WidgetSelected(evt);
						}
					});
				}
				{
					text3 = new Text(group, SWT.PASSWORD|SWT.BORDER);
					text3.setBounds(215, 200, 182, 25);
				}
				{
					label4 = new Label(group, SWT.NONE);
					label4.setText("请再次输入新密码：");
					label4.setBounds(55, 200, 122, 25);
				}
				{
					text2 = new Text(group, SWT.PASSWORD|SWT.BORDER);
					text2.setBounds(215, 130, 182, 24);
				}
				{
					text1 = new Text(group, SWT.PASSWORD|SWT.BORDER);
					text1.setBounds(215, 65, 182, 24);
				}
				{
					label3 = new Label(group, SWT.NONE);
					label3.setText("请输入新密码：");
					label3.setBounds(55, 130, 130, 24);
				}
				{
					label2 = new Label(group, SWT.NONE);
					label2.setText("请输入原密码：");
					label2.setBounds(55, 65, 136, 23);
				}
			}
			{
				FormData tree1LData = new FormData();
				tree1LData.width = 125;
				tree1LData.height = 385;
				tree1LData.left =  new FormAttachment(0, 1000, 0);
				tree1LData.top =  new FormAttachment(1, 1000, 0);
				tree1LData.bottom =  new FormAttachment(1001, 1000, 0);
				tree1LData.right =  new FormAttachment(215, 1000, 0);
				tree1 = new Tree(this, SWT.NONE);
				tree1.setLayoutData(tree1LData);
				tree1.addListener(SWT.MouseDown, new Listener() {
					public void handleEvent(Event event) {
						Point point = new Point(event.x, event.y);
						TreeItem item = tree1.getItem(point);
						if (item != null)
							tree1WidgetSelected(event, item);
					}
				});
				{
					treeItem1 = new TreeItem(tree1, SWT.NONE);
					treeItem1.setText("管理菜单");
					{
						treeItem2 = new TreeItem(treeItem1, SWT.NONE);
						treeItem2.setText("缺陷信息管理");
						{
							treeItem4 = new TreeItem(treeItem2, SWT.NONE);
							treeItem4.setText("缺陷查询");
						}
					}
					{
						treeItem3 = new TreeItem(treeItem1, SWT.NONE);
						treeItem3.setText("个人信息管理");
						{
							treeItem5 = new TreeItem(treeItem3, SWT.NONE);
							treeItem5.setText("密码修改");
						}
					}
					{
						treeItem6 = new TreeItem(treeItem1, SWT.NONE);
						treeItem6.setText("退出系统");
					}
				}
			}
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void button1WidgetSelected(SelectionEvent evt) {
		if (text2.getText().trim().equals("")) {
			box.setMessage("密码不能为空！");
			box.open();
			return;
		}
		if (!text2.getText().trim().equals(text3.getText().trim())) {
			box.setMessage("您两次输入的新密码不一致！");
			box.open();
			return;
		}

		if (Util.isValidPassword(text2.getText().trim()) == false) {
			box.setMessage("新密码格式有误，只能由数字和字母组成，长度为6-12位！");
			box.open();
			return;
		}
		try {
			ArrayList<Object> out = new ArrayList<Object>();
			out.add(userName);
			out.add(text1.getText());
			out.add(this.text2.getText());
			
			SocketConnection.setCmd("modifypwd");
			SocketConnection.setListOut(out);
			SocketConnection.DataTransmission();
			if (SocketConnection.getListIn() == null || SocketConnection.getListIn().size() == 0)
			{
				box.setMessage("服务器连接出错，请检查配置文件和网络连接！");
				box.open();
				//return;
				System.exit(0);
			}
			
			String tag = SocketConnection.getListIn().get(0).toString();
			if(tag.equals("1")){ //修改成功
				box.setMessage("密码修改成功!");
				box.open();
				text1.setText("");
				text2.setText("");
				text3.setText("");
				return;
			}else if(tag.equals("-1")){
				box.setMessage("您输入的原密码不正确！");
				box.open();
				return;
			}else{
				box.setMessage("密码修改失败!");
				box.open();
				return;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private void tree1WidgetSelected(Event evt,TreeItem item) {
		try{
			if(item.getText().equals("缺陷查询")){
				composite1.setVisible(false);
				composite1.setEnabled(false);
				composite2.setVisible(true);
				composite2.setEnabled(true);
				EMQuery.userType = "2";
				EMQuery.userName = ReviewUser.userName;
				EMQuery.showGUI();
			}else if(item.getText().equals("管理菜单")||item.getText().equals("缺陷信息管理")){
				composite1.setVisible(false);
				composite1.setEnabled(false);
				composite2.setVisible(true);
				composite2.setEnabled(true);
				InitViewProList();
				InitBugByProject();
			}else if(item.getText().equals("密码修改") || item.getText().equals("个人信息管理")){
				composite1.setVisible(true);
				composite1.setEnabled(true);
				composite2.setVisible(false);
				composite2.setEnabled(false);
			}else if(item.getText().equals("退出系统")){
			    box1.setMessage("确定要退出吗？");
			    if(box1.open() == SWT.NO)
			    	return;
			    shell.close();
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	private void table1WidgetSelected(SelectionEvent evt) {
		if (table1.getSelection() != null && table1.getSelection().length > 0)
		{
			try{
				EMProcess.ebmsID = table1.getSelection()[0].getText(1);
				if (EMProcess.ebmsID == null || EMProcess.ebmsID.trim().length() == 0)
					return;
				EMProcess.userType = EMQuery.userType;
				EMProcess.userName = EMQuery.userName;
				EMProcess.days = table1.getSelection()[0].getText(5);
				EMProcess.responpeo = table1.getSelection()[0].getText(2);
				EMProcess.status = Util.convertToStatusId(table1.getSelection()[0].getText(3));
				EMProcess.wtgyms = table1.getSelection()[0].getText(4);
				EMProcess.proID = AllProjectId.get(Util.findIdByStr(cboPro.getText(),AllProjectName));
				EMProcess.isEdit = "false";
				EMProcess.showGUI();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	private void InitViewProList(){	//用户信息维护，初始化项目列表
		try {
			AllProjectId.clear();
			AllProjectName.clear();
			SocketConnection.setCmd("getallproject");
			SocketConnection.DataTransmission();
			if (SocketConnection.getListIn() == null || SocketConnection.getListIn().size() == 0)
			{
				box.setMessage("服务器连接出错，请检查配置文件和网络连接！");
				box.open();
				//return;
				System.exit(0);
			}
			
			ArrayList<Object> in = SocketConnection.getListIn();

			if(in.get(0).toString().equals("-1")){
				String items[] ={"无项目"};
				cboPro.setItems(items);
				cboPro.select(0);
			} else{
				int count = in.size()/2;
				String items[] = new String[count];
				for(int i = 0;i < count; i++){
					AllProjectId.add(in.get(2*i).toString());
					AllProjectName.add(in.get(2*i+1).toString());
					items[i] = in.get(2*i + 1).toString();
				}
				cboPro.setItems(items);
			}
			cboPro.select(0);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}		
	}
	
	private void InitBugByProject(){
		try {
			table1.removeAll();
			if(cboPro.getText().equals("无项目"))
				return;
			SocketConnection.setCmd("getbugbyprojectidforreviewuser");
			ArrayList<Object> out = new ArrayList<Object>();
			out.add(AllProjectId.get(Util.findIdByStr(cboPro.getText(),AllProjectName)));
			out.add(userName);
			SocketConnection.setListOut(out);
			SocketConnection.DataTransmission();
			if (SocketConnection.getListIn() == null || SocketConnection.getListIn().size() == 0)
			{
				box.setMessage("服务器连接出错，请检查配置文件和网络连接！");
				box.open();
				//return;
				System.exit(0);
			}
			
			ArrayList<Object> in = SocketConnection.getListIn();
			for(int i=0;i<in.size();i++)
				System.out.println(in.get(i));
			
			Color color = new Color(display,new RGB(222,226,226));
			if(in.get(0).toString().equals("-1")){
				TableItem item = new TableItem(table1, SWT.NONE);
				item.setText(new String[]{"","","无缺陷报告","","",""});
				item.setBackground(color);
				return;
			}
			int bugNum = in.size()/6;
		    for(int i = 0; i < bugNum; i++){
		    	TableItem item = new TableItem(table1, SWT.NONE);
		    	item.setText(new String[] {
		    			String.valueOf(i), 
		    			in.get(i*6).toString(), 
		    			in.get(i*6+2).toString(),
		    			Util.convertToStatusName(in.get(i*6+3).toString()),
		    			in.get(i*6+4).toString(),
		    			in.get(i*6+5).toString()});
		    	if(i%2 == 0)
		    		item.setBackground(color);
		    }
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private void cboProWidgetSelected(SelectionEvent evt) {
		InitBugByProject();
	}
}
